package com.huanwei.login_regist;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.huanwei.cc.R;
import com.huanwei.cc.base.Setting;
import com.huanwei.http.API;
import com.huanwei.http.Okhttp;
import com.huanwei.cc.newbean.LoginT;
import com.huanwei.cc.utils.JSONUtils;
import com.jaredrummler.materialspinner.MaterialSpinner;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册老师页面
 */
public class Regist_t_Activity extends AppCompatActivity {

    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.edit_xuehao)
    EditText editXuehao;
    @BindView(R.id.ed_name)
    EditText edName;
    @BindView(R.id.spinner_yuanxi)
    MaterialSpinner spinnerYuanxi;
    @BindView(R.id.shijian_lay)
    LinearLayout shijianLay;
    @BindView(R.id.spinner_banji)
    MaterialSpinner spinnerBanji;

    @BindView(R.id.spinner_zhicheng)
    MaterialSpinner spinner_zhicheng;

    @BindView(R.id.edit_zhanghao)
    EditText editZhanghao;
    @BindView(R.id.edit_mima)
    EditText editMima;
    @BindView(R.id.but_regist)
    Button butRegist;
    List<String> yuanxilist = Arrays.asList(Setting.yuanxi);
    List<String> banjilist = Arrays.asList(Setting.banji);
    List<String> zhichenglist = Arrays.asList(Setting.zhicheng);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist_ls);
        ButterKnife.bind(this);
        initview();
    }

    String yuanxi;
    String banji;
    String zhicheng;

    private void initview() {

        yuanxi = yuanxilist.get(0);
        spinnerYuanxi.setItems(yuanxilist);
        spinnerYuanxi.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                yuanxi = yuanxilist.get(position);

            }
        });

        banji = banjilist.get(0);
        spinnerBanji.setItems(banjilist);
        spinnerBanji.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                banji = banjilist.get(position);

            }
        });
        zhicheng = zhichenglist.get(0);
        spinner_zhicheng.setItems(zhichenglist);
        spinner_zhicheng.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                zhicheng = zhichenglist.get(position);

            }
        });


    }

    @OnClick({R.id.img_back, R.id.but_regist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.but_regist:
                String Xuehao = editXuehao.getText().toString();
                if (TextUtils.isEmpty(Xuehao)) {
                    Toast.makeText(this, "请输入工号", Toast.LENGTH_SHORT).show();
                    return;
                }
                String Name = edName.getText().toString();
                if (TextUtils.isEmpty(Name)) {
                    Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show();
                    return;
                }
                String Zhanghao = editZhanghao.getText().toString();
                if (TextUtils.isEmpty(Zhanghao)) {
                    Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                String Mima = editMima.getText().toString();
                if (TextUtils.isEmpty(Mima)) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }

                API.Teacherregister(Zhanghao, Mima, Name, Xuehao, zhicheng, yuanxi, banji, new Okhttp.Objectcallback() {
                    @Override
                    public void onsuccess(String st) {



                        LoginT login = JSONUtils.parseJSON(st, LoginT.class);
                        if (login.isIssuccess() == true) {
                            Toast.makeText(Regist_t_Activity.this, "注册成功！", Toast.LENGTH_SHORT).show();

                            finish();
                        } else {
                            Toast.makeText(Regist_t_Activity.this, login.getMsg(), Toast.LENGTH_SHORT).show();

                        }
                    }

                    @Override
                    public void onFalia(int code, String errst) {
                        Toast.makeText(Regist_t_Activity.this, "网络错误！", Toast.LENGTH_SHORT).show();
                    }
                });


                break;
        }
    }
}
